﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BE = Skynet.BusinessEntities;
using Microsoft.Practices.Unity;
using System.Text;
namespace Skynet.MVCWebSite.Areas.Administration.Controllers
{
    public class MgmCustomerController : Controller
    {

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomers BCCustomer { protected  get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomersQueries BCCustomerQueries { get; set; }

        [Dependency]
        public Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService EntityTranslatorService { get; set; }


        //[HttpGet]
        //[OutputCache(Duration = 10, VaryByParam = "none")]        
        //public ActionResult ListCustomers()
        //{
        //    var cust = this.BCCustomer.GetAll();
        //    var model = this.EntityTranslatorService.Translate<IEnumerable<WE.Customer>>(cust);

        //    StringBuilder sb = new StringBuilder();  

        //    foreach (var item in model)
        //    {
        //        sb.AppendLine(item.Name);
                
        //    }

        //    //Content(sb.ToString());
        //    return View();
        //}

        [OutputCache(Duration=20, VaryByParam="none")]
        public ActionResult ListCustomer()
        {
            var cust =  this.BCCustomerQueries.FindCustomers();            
            return View(cust);
        }
        [HttpGet]
        public ActionResult AddCustomer()
        {
            var cust = new BusinessEntities.Customer() { Id = Guid.NewGuid() };
            return View(cust);
        }

        [HttpGet]
        public ActionResult Test()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddCustomer(BusinessEntities.Customer customer)        
        {

            if (this.ModelState.IsValid)
            {
                var target = this.EntityTranslatorService.Translate<BE.Customer>(customer);
                this.BCCustomer.MaintenanceCustomers(target);  
                TempData["message"] =  "add customer success";
                return RedirectToAction("ListCustomer");
            }
            return View();  
        }

    }
}
